import Taro from "@tarojs/taro";
import { View, Button, Image, Text } from "@tarojs/components";
import { useEffect } from "react";
import { useDispatch } from 'react-redux';
import { addUserInfo } from './slice';
import { picture } from './image';
import css from "./index.module.less";

export default function Authorization() {
    useEffect(() => {
        Taro.login({
            success: (res) => {
                console.log(res);
                const appid = "wx41df3076f5aec307";
                const secret = "949a05072d7f0cd7e81e8320c75d46b8";
                Taro.request({
                    url: `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${res.code}&grant_type=authorization_code`, //仅为示例，并非真实的接口地址
                    // header: {
                    //     'content-type': 'application/json' // 默认值
                    // },
                    method: 'GET',
                    success: function (data) {
                        console.log(data);

                    }
                });
            }
        });

    });
    const dispatch = useDispatch();
    const getUserProfile = () => {
        Taro.getUserProfile({
            desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                dispatch(addUserInfo(res));
                Taro.switchTab({ url: "../index/index" });
            },
            fail: (error) => {
                console.log("失败", error);
            }
        });
    };
    return (
        <View className={css.app}>
            <Image className={css.img} src={picture}>
            </Image>
            <Text className={css.text}>此页面是党旗飘飘授权页面，点击授权即可</Text>
            <Button
              openType='getUserInfo' onClick={getUserProfile}
              plain
              className={css.but}
            >
                授权
            </Button>
        </View>
    );

}